import Vue from 'vue'
import ElementUI from 'element-ui'
// import VueRouter from 'vue-router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import 'element-ui/lib/theme-default/index.css';
import App from './App.vue'
// import routes from './router/router.js'
import router from './router';

import group from './common/group';

import store from "./vuex/store"
let main = Vue.extend({
  store,
  components: {
    'alert': alert
  }
})

Vue.use(group);
Vue.use(ElementUI);
// Vue.use(VueRouter);
Vue.use(VueAxios, axios)

router.beforeEach((to, from, next) => {
  // store.commit("increment")

  function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
      return unescape(arr[2]);
    else
      return null;
  }

  //导航设置
  store.commit("currentNav", to.fullPath);

  if (!to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (getCookie("data")) {  // 通过vuex state获取当前的token是否存在
      let data = JSON.parse(getCookie("data"));

      if (data && !store.state.grogshop.hotelId) {
        store.commit("increment", JSON.parse(getCookie("data")));
        store.commit("grogshopMessage", store.state.login.belong_name)
      }

      if (data.user_type === 1) {
        if (to.fullPath == "/inn" || to.fullPath == "/account" || to.fullPath == "/accountAdd" || to.fullPath == "/accountUpdate" || to.fullPath == "/groupAdd") {
          next({
            path: '/account',
            query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
          })
        }
      }

      
      next();
    }
    else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
});
// const router = new VueRouter({
//   routes
// });

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');